package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.ICompanyDao;
import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.dto.CommonModelDto;
import com.cn.wanxi.dto.CompanyModelDto;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.ICommonService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class CommonServiceImpl implements ICommonService {


    public CommonServiceImpl() {
        //将数据新增到数据库
//        ICompanyDao iCompanyDao=new CompanyImpl();
//        CompanyModel companyModel=new CompanyModel();
//
//        iCompanyDao.addCompany(companyModel);
    }

    /**
     * 封装公共信息
     *
     * @return
     */
    @Override
    public CommonModelDto getCommonModelDto() {
        CommonModelDto commonModelDto = new CommonModelDto();
        final CompanyModel[] companyModel = {null};
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = getCompanyModel();
                    System.out.println("线程" + finalI);
                    try {
                        Thread.sleep(10);//睡眠10毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "线程" + i);//name就是可以定义线程名字
            thread.start();
        }
        //得到公司信息
        commonModelDto.setCompanyModel(companyModel[0]);
        //得到导航信息
        commonModelDto.setNavList(getNavModelList());
        return commonModelDto;
    }

    /**
     * 得到公司信息
     * 第一次需要从数据库里面去取，第二次以后需要从缓存里面取
     * 所以需要用到redis，即先导包
     * synchronized同步
     *
     * @return
     */
    private CompanyModel getCompanyModel() {
        ICompanyDao iCompanyDao = new CompanyImpl();

        Jedis jedis = new Jedis();//引入redis代码
//         public Jedis(String host, int port) host指地址 http://127.0.0.1:/ port 端口 6379
//        public Jedis(String host)
//         public Jedis()
//        我们需要将公司信息存入缓存,redis现在没有直接存储对象，所以必须分开进行存储
//如果缓存里面没有数据就查询数据库，如果有则从缓存里面去读取
        String companyId = jedis.get("companyId");//companyId == null当id为null的情况下说明缓存里面没有值，我们必须读取数据库
        boolean cunzai = jedis.exists("companyId");//判断id是否存在
        CompanyModel companyModel = null;
        if (cunzai) {
//            如果存在，则我们需要封装公司信息companyModel
            companyModel = new CompanyModel();//创建新的实例化对象，里面所有数据为null
            companyModel.setId(Integer.parseInt(companyId));
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setAd(jedis.get("ad"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setVersion(jedis.get("version"));
        } else {
            companyModel = iCompanyDao.getCommonModelDto();//从数据库里面读取的
//            jedis.set("companyId", companyModel.getId().toString());
//            jedis.set("companyName", companyModel.getName());
//            jedis.set("companyLogo", companyModel.getLogo());
//            jedis.set("companyAddress", companyModel.getAddress());
//我们的信息可能随时改变，所以需要更新我们的缓存，
// 1，手动删除
//            jedis.flushAll();//删除所有缓存
//            jedis.flushDB();//删除当前数据库的缓存
// 2.自动删除,就是设置一个过期时间
            jedis.set("companyId", companyModel.getId().toString());
            jedis.set("ad", companyModel.getAd());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("logo", companyModel.getLogo());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("address", companyModel.getAddress());
            jedis.set("version", companyModel.getVersion());
        }
        //要得到公司信息，必须新增数据
        return companyModel;
    }

    /**
     * 查询导航信息
     * 首先去缓存里面读取，如果有值，则读取缓存，没有，则读取数据库
     *
     * @return
     */
    private List<NavModel> getNavModelList() {

        NavDao navDao = new NavImpl();
        Jedis jedis = new Jedis();
        List<NavModel> list = new ArrayList<>();
//        boolean cunzai = jedis.exists("navTitle");
        Long length = jedis.llen("navTitle");
//        如果length的长度大于0，表示，缓存里面有值，否则读取数据库
        if (length > 0) {
            List<String> hrefs = jedis.lrange("navHref", 0, -1);
            List<String> titles = jedis.lrange("navTitle", 0, -1);
            for (int i = 0; i < hrefs.size(); i++) {
                NavModel navModel = new NavModel();
//                navModel.setTitle(jedis.lpop("navTitle"));//这种方式只能做一次因为下一次就没有了，别删除
//                navModel.setHref(jedis.lpop("navHref"));
                navModel.setHref(hrefs.get(i));
                navModel.setTitle(titles.get(i));
                list.add(navModel);
            }
        } else {
            list = navDao.getNavModelList();
            for (int i = 0; i < list.size(); i++) {
                jedis.lpush("navTitle", list.get(i).getTitle());
                jedis.lpush("navHref", list.get(i).getHref());
            }
        }

        return list;
    }
}
